import resources.Constants;

import registnet.dao.CoursePeer;
import registnet.dao.Course;
import registnet.dao.SectionPeer;
import registnet.dao.Section;
import registnet.dao.SectionConfigurationPeer;
import registnet.dao.SectionConfiguration;
import registnet.dao.UnitPeer;

SectionConfigurationPeer scp = new SectionConfigurationPeer();
Section section;

String _instance = "section";
String _instances = "sections";

String _proceed = bundle.getProperty("registnet.messages.proceed");
String _check = bundle.getProperty("registnet.messages.check");
String _successful = bundle.getProperty("registnet.messages.successful");
String _failed = bundle.getProperty("registnet.messages.failed");
String _select_one = bundle.getProperty("registnet.messages.select_one");
String _selected = bundle.getProperty("registnet.messages.selected");
String _use_search_result_viewer = bundle.getProperty("registnet.messages.use_search_result_viewer");


boolean isShowingInfo;

/* [start]	prepares the tree */
ArrayList parentList = new ArrayList();
Map mapSTN_parent = new HashMap();
Map mapAL_parent = new HashMap();
Map mapAL_child = new HashMap();
Map map_treeitem = new HashMap();
SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
/* [end] */



/* [start]	defines the renderer for the tree */
public class MyTreeitemRenderer implements TreeitemRenderer {
	public void render(Treeitem item, Object data) throws Exception {
		String str = data.toString();
		Treerow tr = null;
		Treecell tc = new Treecell(str);
		if(item.getTreerow()==null){
			tr = new Treerow();
			tr.setParent(item);
			map_treeitem.put(str, item);
		}else{
			tr = item.getTreerow(); 
			tr.getChildren().clear();
		}
		tc.setParent(tr);
	}
}
/* [end] */



/* [start]	retrieves the units and places them on the tree;
			limits the courses to retrieve depending on the user type */
			
/*
public void retrieveObjects(){
	map_treeitem.clear();
	mapSTN_parent.clear();
	mapAL_parent.clear();
	mapAL_child.clear();
	parentList.clear();
	
	List temp = new SectionPeer().getSectionsBySectionConfiguration(config_id.value);
	List courses = new Vector();
	HashMap course_section = new HashMap();
	
	for(int i=0; i<temp.size(); i++){
		Section section = temp.get(i);
		if (!courses.contains(section.getCourse().getCourseCode()))
			courses.add(section.getCourse().getCourseCode());
		if (!course_section.containsKey(section.getCourse().getCourseCode())){
			List list = new Vector();
			list.add(section.getSectionName());
			course_section.put(section.getCourse().getCourseCode(), list);
		}
		else{
			course_section.get(section.getCourse().getCourseCode()).add(section.getSectionName());
		}
	}
	
	for(int i=0; i<courses.size(); i++){
		String course = courses.get(i);
		Iterator iter = course_section.keySet().iterator();
		ArrayList alChild = new ArrayList();
		ArrayList alLeaf = new ArrayList();
		List sections = course_section.get(course);
		for (int j=0; j<sections.size(); j++){
			SimpleTreeNode stnChild = new SimpleTreeNode(sections.get(j), new ArrayList());
			alLeaf.add(sections.get(j));
			alChild.add(stnChild);
		}
		SimpleTreeNode stnParent = new SimpleTreeNode(course, alChild);
		mapSTN_parent.put(course, stnParent);
		mapAL_parent.put(course, alChild);
		mapAL_child.put(course, alLeaf);
		parentList.add(stnParent);
	}
	
	SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
	SimpleTreeModel stm = new SimpleTreeModel(root);
	instance_tree.setModel(stm);
	instance_tree.setTreeitemRenderer(new MyTreeitemRenderer());
}*/

public void retrieveObjects(){
	map_treeitem.clear();
	mapSTN_parent.clear();
	mapAL_parent.clear();
	mapAL_child.clear();
	parentList.clear();
	List courses = new CoursePeer().getCoursesByUnit(new UnitPeer().getUnitId(_office));
	for(int i=0; i<courses.size(); i++){
		List sections = new SectionPeer().getLectureSections(courses.get(i).getCourseId(), config_id.value);
		ArrayList alChild = new ArrayList();
		ArrayList alLeaf = new ArrayList();
		for(int j=0; j<sections.size(); j++){
			SimpleTreeNode stnChild = new SimpleTreeNode(sections.get(j).getSectionName(), new ArrayList());
			alLeaf.add(sections.get(j).getSectionName());
			alChild.add(stnChild);
			List labs = new SectionPeer().getLaboratory( config_id.value, courses.get(i).getCourseId(), sections.get(j).getSectionName());
			for(int k=0; k<labs.size(); k++){
				SimpleTreeNode stnChild1 = new SimpleTreeNode(labs.get(k).getSectionName(), new ArrayList());
				alLeaf.add(labs.get(k).getSectionName());
				alChild.add(stnChild1);
			}
		}
		SimpleTreeNode stnParent = new SimpleTreeNode(courses.get(i).getCourseCode(), alChild);
		mapSTN_parent.put(courses.get(i).getCourseCode(), stnParent);
		mapAL_parent.put(courses.get(i).getCourseCode(), alChild);
		mapAL_child.put(courses.get(i).getCourseCode(), alLeaf);
		parentList.add(stnParent);
	}
	SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
	SimpleTreeModel stm = new SimpleTreeModel(root);
	instance_tree.setModel(stm);
	instance_tree.setTreeitemRenderer(new MyTreeitemRenderer());
	
}
/* [end] */



String _add = bundle.getProperty("registnet.module.data_management.actions.menu.add");
String _edit = bundle.getProperty("registnet.module.data_management.actions.menu.edit");
String _delete = bundle.getProperty("registnet.module.data_management.actions.menu.delete");
String _save = bundle.getProperty("registnet.module.data_management.macro.colleges.form.save");
String _cancel = bundle.getProperty("registnet.module.data_management.macro.colleges.form.cancel");

List courses = new CoursePeer().getCoursesByUnit(new UnitPeer().getUnitId(_office));


void addLecture(){
	isShowingInfo=false;
	try{
		contents.removeChild(contents.getFirstChild());
	}catch(Exception e){}
	Component win = Executions.createComponents("/WEB-INF/macros/data_management/section_lecture.zul", contents, null);
	title.setValue("Add Lecture");
	instance_tree.clearSelection();
	win.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener("Add", "Lecture"));
	win.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
}

void addLaboratory(){
	isShowingInfo=false;
	try{
		contents.removeChild(contents.getFirstChild());
	}catch(Exception e){}
	Component win = Executions.createComponents("/WEB-INF/macros/data_management/section_lab.zul", contents, null);
	title.setValue("Add Laboratory");
	instance_tree.clearSelection();
	win.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener("Add", "Laboratory"));
	win.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
}

void addRecitation(){
	isShowingInfo=false;
	try{
		contents.removeChild(contents.getFirstChild());
	}catch(Exception e){}
	Component win = Executions.createComponents("/WEB-INF/macros/data_management/section_lab.zul", contents, null);
	title.setValue("Add Recitation");
	instance_tree.clearSelection();
	win.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener("Add", "Recitation"));
	win.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
}


/* [start]	defines an event listener for the submit button */
private class SubmitEventListener implements EventListener {
	String action;
	String type;
	public SubmitEventListener (String action, String type){
		this.action = action;
		this.type = type; 
	}
	public void onEvent(Event event) throws UiException {
		if(this.action.equals(_add))
			createNewSection(type);
		else if (this.action.equals(_edit))
			editSection(type);
	}
}
/* [end] */


/* [start]	defines an event listener for the items on the tree;
			when the item is selected, its info is displayed */
private class SelectEventListener implements EventListener {
	public SelectEventListener (){}
	public void onEvent(Event event) throws UiException {
		showInfo();
	}
}
/* [end] */


/* [start]	defines an event listener for the cancel button;
			when fired, it deselects the items on the tree */
private class CancelEventListener implements EventListener {
	public SubmitEventListener (){}
	public void onEvent(Event event) throws UiException {
		instance_tree.clearSelection();
		try{
			title.setValue("");
			contents.removeChild(contents.getFirstChild());
		} catch (Exception e) {}
	}
}
/* [end] */



/* [start]	creates a new entry;
			updates the tree */
void createNewSection(String type){

	if (type.equals("Lecture")){
		try{
			String courseCodeValue;
			String sectionNameValue;
			String dayValue; 
			String timeValue;
			String roomValue;
			String instructorValue;
			Integer studentCountValue;
			
			if (course_code.value.trim().length() != 0)
				courseCodeValue = course_code.value;
			else{
				alert("Invalid course");
				return;
			}
			
			if (section_name.value.trim().length() != 0)
				sectionNameValue = section_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			
			if (day.value.trim().length() != 0)
				dayValue = day.value;
			else{
				alert("Invalid day");
				return;
			}
			
			
			if(!start_hrs.getValue().equals("") && !start_mins.getValue().equals("") && !end_hrs.getValue().equals("") && !end_mins.getValue().equals("")){
				if(start_mins.getValue().equals("00"))
					timeValue = start_hrs.getValue();
				else
					timeValue = start_hrs.getValue() + ":" + start_mins.getValue();
					
				if(end_mins.getValue().equals("00"))
					timeValue = timeValue + "-" + end_hrs.getValue();
				else
					timeValue = timeValue + "-" + end_hrs.getValue() + ":" + end_mins.getValue();
			}
			else{
				alert("Invalid time");
				return;
			}
			
			if (room.value.trim().length() != 0)
				roomValue = room.value;
			else{
				alert("Invalid room");
				return;
			}
			
			
			if (instructor.value.trim().length() != 0)
				instructorValue = instructor.value;
			else{
				alert("Invalid instructor");
				return;
			}
			
			
			if (student_count.value != null && student_count.value > 0)
				studentCountValue = student_count.value;
			else{
				alert("Invalid number");
				return;
			}
			
			if (new SectionPeer().createNewLectureSection(config_id.value, new CoursePeer().getCourseId(courseCodeValue), sectionNameValue, dayValue, roomValue, timeValue, instructorValue, studentCountValue, team_teach.getSelectedIndex()) != null){
				title.setValue("Add Lecture Section successful");
				SimpleTreeNode newNode = new SimpleTreeNode(section_name.value, new ArrayList());
				String parentValue = course_code.value;
				mapAL_parent.get(parentValue).add(newNode);
				mapAL_child.get(parentValue).add(section_name.value);
				if(mapAL_parent.get(parentValue).size() == 1){
					instance_tree.renderItem(map_treeitem.get(parentValue));
					map_treeitem.get(parentValue).setOpen(true);
				}
				else
					instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_parent.get(parentValue).size() - 1, mapAL_parent.get(parentValue).size() - 1, org.zkoss.zul.event.TreeDataEvent.INTERVAL_ADDED);
				try{
					contents.removeChild(contents.getFirstChild());
				}catch(Exception e){}
			}
			else{
				alert("Check information supplied");
			}
			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	else if (type.equals("Laboratory")){
		try{
			String courseCodeValue;
			String sectionNameValue;
			String labNameValue;
			String dayValue; 
			String timeValue;
			String roomValue;
			String instructorValue;
			Integer studentCountValue;
			
			if (course_code.value.trim().length() != 0)
				courseCodeValue = course_code.value;
			else{
				alert("Invalid course");
				return;
			}
			
			if (section_name.value.trim().length() != 0)
				sectionNameValue = section_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			if (laboratory_name.value.trim().length() != 0)
				labNameValue = laboratory_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			String labName = sectionNameValue +"-"+labNameValue;
			if (day.value.trim().length() != 0)
				dayValue = day.value;
			else{
				alert("Invalid day");
				return;
			}
			
			
			if(!start_hrs.getValue().equals("") && !start_mins.getValue().equals("") && !end_hrs.getValue().equals("") && !end_mins.getValue().equals("")){
				if(start_mins.getValue().equals("00"))
					timeValue = start_hrs.getValue();
				else
					timeValue = start_hrs.getValue() + ":" + start_mins.getValue();
					
				if(end_mins.getValue().equals("00"))
					timeValue = timeValue + "-" + end_hrs.getValue();
				else
					timeValue = timeValue + "-" + end_hrs.getValue() + ":" + end_mins.getValue();
			}
			else{
				alert("Invalid time");
				return;
			}
			
			if (room.value.trim().length() != 0)
				roomValue = room.value;
			else{
				alert("Invalid room");
				return;
			}
			
			
			if (instructor.value.trim().length() != 0)
				instructorValue = instructor.value;
			else{
				alert("Invalid instructor");
				return;
			}
			
			
			if (student_count.value != null && student_count.value > 0)
				studentCountValue = student_count.value;
			else{
				alert("Invalid number");
				return;
			}
			
			if (new SectionPeer().createLaboratorySection(config_id.value, new CoursePeer().getCourseId(courseCodeValue), labName, dayValue, roomValue, timeValue, instructorValue, studentCountValue, team_teach.getSelectedIndex()) != null){
				title.setValue("Add Laboratory Section successful");
				SimpleTreeNode newNode = new SimpleTreeNode(labName, new ArrayList());
				String parentValue = course_code.value;
				List temp = new SectionPeer().getLaboratory(config_id.value, new CoursePeer().getCourseId(course_code.value), sectionNameValue);
				int index = mapAL_child.get(parentValue).indexOf(sectionNameValue)+temp.size();
				mapAL_parent.get(parentValue).add(index, newNode);
				mapAL_child.get(parentValue).add(index, labName);
				if(mapAL_parent.get(parentValue).size() == 1){
					instance_tree.renderItem(map_treeitem.get(parentValue));
					map_treeitem.get(parentValue).setOpen(true);
				}
				else
					instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), index, index, org.zkoss.zul.event.TreeDataEvent.INTERVAL_ADDED);
				try{
					contents.removeChild(contents.getFirstChild());
				}catch(Exception e){}
			}
			else{
				alert("Check information supplied");
			}
		}catch(Exception e){
			e.printStackTrace();
			alert("Check information supplied");
		}
	}
	
	else if (type.equals("Recitation")){
		try{
			String courseCodeValue;
			String sectionNameValue;
			String labNameValue;
			String dayValue; 
			String timeValue;
			String roomValue;
			String instructorValue;
			Integer studentCountValue;
			
			if (course_code.value.trim().length() != 0)
				courseCodeValue = course_code.value;
			else{
				alert("Invalid course");
				return;
			}
			
			if (section_name.value.trim().length() != 0)
				sectionNameValue = section_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			if (laboratory_name.value.trim().length() != 0)
				labNameValue = laboratory_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			String labName = sectionNameValue +"-"+labNameValue;
			if (day.value.trim().length() != 0)
				dayValue = day.value;
			else{
				alert("Invalid day");
				return;
			}
			
			
			if(!start_hrs.getValue().equals("") && !start_mins.getValue().equals("") && !end_hrs.getValue().equals("") && !end_mins.getValue().equals("")){
				if(start_mins.getValue().equals("00"))
					timeValue = start_hrs.getValue();
				else
					timeValue = start_hrs.getValue() + ":" + start_mins.getValue();
					
				if(end_mins.getValue().equals("00"))
					timeValue = timeValue + "-" + end_hrs.getValue();
				else
					timeValue = timeValue + "-" + end_hrs.getValue() + ":" + end_mins.getValue();
			}
			else{
				alert("Invalid time");
				return;
			}
			
			if (room.value.trim().length() != 0)
				roomValue = room.value;
			else{
				alert("Invalid room");
				return;
			}
			
			
			if (instructor.value.trim().length() != 0)
				instructorValue = instructor.value;
			else{
				alert("Invalid instructor");
				return;
			}
			
			
			if (student_count.value != null && student_count.value > 0)
				studentCountValue = student_count.value;
			else{
				alert("Invalid number");
				return;
			}
			
			if (new SectionPeer().createRecitationSection(config_id.value, new CoursePeer().getCourseId(courseCodeValue), labName, dayValue, roomValue, timeValue, instructorValue, studentCountValue, team_teach.getSelectedIndex()) != null){
				title.setValue("Add Recitation Section successful");
				SimpleTreeNode newNode = new SimpleTreeNode(labName, new ArrayList());
				String parentValue = course_code.value;
				List temp = new SectionPeer().getLaboratory(config_id.value, new CoursePeer().getCourseId(course_code.value), sectionNameValue);
				int index = mapAL_child.get(parentValue).indexOf(sectionNameValue)+temp.size();
				mapAL_parent.get(parentValue).add(index, newNode);
				mapAL_child.get(parentValue).add(index, labName);
				if(mapAL_parent.get(parentValue).size() == 1){
					instance_tree.renderItem(map_treeitem.get(parentValue));
					map_treeitem.get(parentValue).setOpen(true);
				}
				else
					instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), index, index, org.zkoss.zul.event.TreeDataEvent.INTERVAL_ADDED);
				try{
					contents.removeChild(contents.getFirstChild());
				}catch(Exception e){}
			}
			else{
				alert("Check information supplied");
			}
		}catch(Exception e){
			e.printStackTrace();
			alert("Check information supplied");
		}
	}
	
}
/* [end] */



void showInfo(){
	
	isShowingInfo = true;
	
	if (contents.getFirstChild() != null){
		contents.removeChild(contents.getFirstChild());
	}
	
	try{
		if (instance_tree.getSelectedCount() == 1 && !(instance_tree.getSelectedItem().getParent().getParent() instanceof org.zkoss.zul.Tree)){
			try{
			
				section = new SectionPeer().getSection(config_id.value, new CoursePeer().getCourseId(instance_tree.getSelectedItem().getParent().getParent().getLabel()), instance_tree.getSelectedItem().getLabel());
				if(section.getLaboratoryDay() != null && section.getLaboratoryDay().trim().length() != 0){
					Component show_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/section_lab.zul", contents, null);
					title.setValue(section.getCourse().getCourseCode()+" "+section.getSectionName());
					show_instance_form.getFellow("course_code_l").setValue(section.getCourse().getCourseCode());
					show_instance_form.getFellow("section_name_l").setValue(section.getSectionName());
					show_instance_form.getFellow("day_l").setValue(section.getLaboratoryDay());
					show_instance_form.getFellow("time_l").setValue(section.getLaboratoryTime());
					show_instance_form.getFellow("room_l").setValue(section.getLaboratoryRoom());
					show_instance_form.getFellow("instructor_l").setValue(section.getLaboratoryInstructor());
					show_instance_form.getFellow("student_count_l").setValue(section.getStudentCount().toString());
					show_instance_form.getFellow("team_teach_l").setValue(Constants.MAP_ANSWER_VALUE.get(section.getIsTeamTeach()));
					show_instance_form.getFellow("save_btn").detach();
					show_instance_form.getFellow("cancel_btn").detach();
				}
				else if(section.getRecitationDay() != null && section.getRecitationDay().trim().length() != 0){
					Component show_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/section_lab.zul", contents, null);
					title.setValue(section.getCourse().getCourseCode()+" "+section.getSectionName());
					show_instance_form.getFellow("course_code_l").setValue(section.getCourse().getCourseCode());
					show_instance_form.getFellow("section_name_l").setValue(section.getSectionName());
					show_instance_form.getFellow("day_l").setValue(section.getRecitationDay());
					show_instance_form.getFellow("time_l").setValue(section.getRecitationTime());
					show_instance_form.getFellow("room_l").setValue(section.getRecitationRoom());
					show_instance_form.getFellow("instructor_l").setValue(section.getRecitationInstructor());
					show_instance_form.getFellow("student_count_l").setValue(section.getStudentCount().toString());
					show_instance_form.getFellow("team_teach_l").setValue(Constants.MAP_ANSWER_VALUE.get(section.getIsTeamTeach()));
					show_instance_form.getFellow("save_btn").detach();
					show_instance_form.getFellow("cancel_btn").detach();
				}
				else if(section.getLectureDay() != null && section.getLectureDay().trim().length() != 0){
					Component show_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/section_lecture.zul", contents, null);
					title.setValue(section.getCourse().getCourseCode()+" "+section.getSectionName());
					show_instance_form.getFellow("course_code_l").setValue(section.getCourse().getCourseCode());
					show_instance_form.getFellow("section_name_l").setValue(section.getSectionName());
					show_instance_form.getFellow("day_l").setValue(section.getLectureDay());
					show_instance_form.getFellow("time_l").setValue(section.getLectureTime());
					show_instance_form.getFellow("room_l").setValue(section.getLectureRoom());
					show_instance_form.getFellow("instructor_l").setValue(section.getLectureInstructor());
					show_instance_form.getFellow("student_count_l").setValue(section.getStudentCount().toString());
					show_instance_form.getFellow("team_teach_l").setValue(Constants.MAP_ANSWER_VALUE.get(section.getIsTeamTeach()));
					show_instance_form.getFellow("save_btn").detach();
					show_instance_form.getFellow("cancel_btn").detach();
				}
			}catch(Exception e){}
		}
		else if (instance_tree.getSelectedCount() > 1)
			title.setValue("" + instance_tree.getSelectedCount() + " " + _instance + " " + _selected);
		else
			title.setValue("");
			
	}catch (Exception e) { title.setValue(""); }
}

void edit(){
	isShowingInfo = false;
	isSearching = false;
	try{
		contents.removeChild(contents.getFirstChild());
	} catch (Exception e) {}
	if(instance_tree.getSelectedCount() == 1){
		if(section.getLaboratoryDay() != null && section.getLaboratoryDay().trim().length() != 0){
			Component show_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/section_lab.zul", contents, null);
			title.setValue(section.getCourse().getCourseCode()+" "+section.getSectionName());
			show_instance_form.getFellow("course_code").setValue(section.getCourse().getCourseCode());
			show_instance_form.getFellow("course_code").setDisabled(true);
			String[] sectionN = section.getSectionName().split("-");
			show_instance_form.getFellow("section_name").setValue(sectionN[0]);
			show_instance_form.getFellow("section_name").setDisabled(true);
			show_instance_form.getFellow("laboratory_name").setValue(sectionN[1]);
			show_instance_form.getFellow("day").setValue(section.getLaboratoryDay());
			
			String[] time = section.getLaboratoryTime().split("-");
			if(time[0].contains(":")){
				String[] t = time[0].split(":");
				show_instance_form.getFellow("start_hrs").setValue(t[0]);
				show_instance_form.getFellow("start_mins").setValue(t[1]);
			}
			else{
				show_instance_form.getFellow("start_hrs").setValue(time[0]);
				show_instance_form.getFellow("start_mins").setValue("00");
			}
			
			if(time[1].contains(":")){
				String[] t = time[1].split(":");
				show_instance_form.getFellow("end_hrs").setValue(t[0]);
				show_instance_form.getFellow("end_mins").setValue(t[1]);
			}
			else{
				show_instance_form.getFellow("end_hrs").setValue(time[1]);
				show_instance_form.getFellow("end_mins").setValue("00");
			}
			
			show_instance_form.getFellow("room").setValue(section.getLaboratoryRoom());
			show_instance_form.getFellow("instructor").setValue(section.getLaboratoryInstructor());
			show_instance_form.getFellow("student_count").setValue(section.getStudentCount());
			show_instance_form.getFellow("team_teach").setValue(Constants.MAP_ANSWER_VALUE.get(section.getIsTeamTeach()));
			show_instance_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener("Edit", "Laboratory"));
			show_instance_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
		}
		else if(section.getRecitationDay() != null && section.getRecitationDay().trim().length() != 0){
			Component show_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/section_lab.zul", contents, null);
			title.setValue(section.getCourse().getCourseCode()+" "+section.getSectionName());
			show_instance_form.getFellow("course_code").setValue(section.getCourse().getCourseCode());
			show_instance_form.getFellow("course_code").setDisabled(true);
			String[] sectionN = section.getSectionName().split("-");
			show_instance_form.getFellow("section_name").setValue(sectionN[0]);
			show_instance_form.getFellow("section_name").setDisabled(true);
			show_instance_form.getFellow("day").setValue(section.getRecitationDay());
			
			String[] time = section.getRecitationTime().split("-");
			if(time[0].contains(":")){
				String[] t = time[0].split(":");
				show_instance_form.getFellow("start_hrs").setValue(t[0]);
				show_instance_form.getFellow("start_mins").setValue(t[1]);
			}
			else{
				show_instance_form.getFellow("start_hrs").setValue(time[0]);
				show_instance_form.getFellow("start_mins").setValue("00");
			}
			
			if(time[1].contains(":")){
				String[] t = time[1].split(":");
				show_instance_form.getFellow("end_hrs").setValue(t[0]);
				show_instance_form.getFellow("end_mins").setValue(t[1]);
			}
			else{
				show_instance_form.getFellow("end_hrs").setValue(time[1]);
				show_instance_form.getFellow("end_mins").setValue("00");
			}
			
			show_instance_form.getFellow("room").setValue(section.getRecitationRoom());
			show_instance_form.getFellow("instructor").setValue(section.getRecitationInstructor());
			show_instance_form.getFellow("student_count").setValue(section.getStudentCount());
			show_instance_form.getFellow("team_teach").setValue(Constants.MAP_ANSWER_VALUE.get(section.getIsTeamTeach()));
			show_instance_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener("Edit", "Recitation"));
			show_instance_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
		}
		else if(section.getLectureDay() != null && section.getLectureDay().trim().length() != 0){
			Component show_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/section_lecture.zul", contents, null);
			title.setValue(section.getCourse().getCourseCode()+" "+section.getSectionName());
			show_instance_form.getFellow("course_code").setValue(section.getCourse().getCourseCode());
			show_instance_form.getFellow("course_code").setDisabled(true);
			show_instance_form.getFellow("section_name").setValue(section.getSectionName());
			show_instance_form.getFellow("day").setValue(section.getLectureDay());
			
			String[] time = section.getLectureTime().split("-");
			if(time[0].contains(":")){
				String[] t = time[0].split(":");
				show_instance_form.getFellow("start_hrs").setValue(t[0]);
				show_instance_form.getFellow("start_mins").setValue(t[1]);
			}
			else{
				show_instance_form.getFellow("start_hrs").setValue(time[0]);
				show_instance_form.getFellow("start_mins").setValue("00");
			}
			
			if(time[1].contains(":")){
				String[] t = time[1].split(":");
				show_instance_form.getFellow("end_hrs").setValue(t[0]);
				show_instance_form.getFellow("end_mins").setValue(t[1]);
			}
			else{
				show_instance_form.getFellow("end_hrs").setValue(time[1]);
				show_instance_form.getFellow("end_mins").setValue("00");
			}
			
			show_instance_form.getFellow("room").setValue(section.getLectureRoom());
			show_instance_form.getFellow("instructor").setValue(section.getLectureInstructor());
			show_instance_form.getFellow("student_count").setValue(section.getStudentCount());
			show_instance_form.getFellow("team_teach").setValue(Constants.MAP_ANSWER_VALUE.get(section.getIsTeamTeach()));
			show_instance_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener("Edit", "Lecture"));
			show_instance_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
		}
	}
	else{
		alert(_select_one + " " + _instance);
	}
}

editSection(String type){
	if (type.equals("Lecture")){
		try{
			String courseCodeValue;
			String sectionNameValue;
			String dayValue; 
			String timeValue;
			String roomValue;
			String instructorValue;
			Integer studentCountValue;
			
			if (course_code.value.trim().length() != 0)
				courseCodeValue = course_code.value;
			else{
				alert("Invalid course");
				return;
			}
			
			if (section_name.value.trim().length() != 0)
				sectionNameValue = section_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			
			if (day.value.trim().length() != 0)
				dayValue = day.value;
			else{
				alert("Invalid day");
				return;
			}
			
			
			if(!start_hrs.getValue().equals("") && !start_mins.getValue().equals("") && !end_hrs.getValue().equals("") && !end_mins.getValue().equals("")){
				if(start_mins.getValue().equals("00"))
					timeValue = start_hrs.getValue();
				else
					timeValue = start_hrs.getValue() + ":" + start_mins.getValue();
					
				if(end_mins.getValue().equals("00"))
					timeValue = timeValue + "-" + end_hrs.getValue();
				else
					timeValue = timeValue + "-" + end_hrs.getValue() + ":" + end_mins.getValue();
			}
			else{
				alert("Invalid time");
				return;
			}
			
			if (room.value.trim().length() != 0)
				roomValue = room.value;
			else{
				alert("Invalid room");
				return;
			}
			
			
			if (instructor.value.trim().length() != 0)
				instructorValue = instructor.value;
			else{
				alert("Invalid instructor");
				return;
			}
			
			
			if (student_count.value != null && student_count.value > 0)
				studentCountValue = student_count.value;
			else{
				alert("Invalid number");
				return;
			}
			
			if (new SectionPeer().updateLectureSection(section.getSectionId(), sectionNameValue, dayValue, roomValue, timeValue, instructorValue, studentCountValue, team_teach.getSelectedIndex())){
				String parentValue;
				parentValue = course_code.value;
			
				if(!sectionNameValue.equals(instance_tree.getSelectedItem().getLabel())){
					List labs = new SectionPeer().getLaboratory(config_id.value, new CoursePeer().getCourseId(parentValue),instance_tree.getSelectedItem().getLabel());
					for(int i=0; i<labs.size(); i++){
						String labName = sectionNameValue+"-"+labs.get(i).getSectionName().split("-")[1];
						if (new SectionPeer().updateLaboratorySection(labs.get(i).getSectionId(), labName)){
							SimpleTreeNode newNode1 = new SimpleTreeNode(labName, new ArrayList());
							mapAL_parent.get(parentValue).set(mapAL_child.get(parentValue).indexOf(labs.get(i).getSectionName()), newNode1);
							mapAL_child.get(parentValue).set(mapAL_child.get(parentValue).indexOf(labs.get(i).getSectionName()), labName);
							instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_child.get(parentValue).indexOf(labName), mapAL_child.get(parentValue).indexOf(labName), org.zkoss.zul.event.TreeDataEvent.CONTENTS_CHANGED);
						}
						else{
							alert("Unable to update all its laboratory sections.");
							break;
						}
					}
				}
				
				SimpleTreeNode newNode = new SimpleTreeNode(sectionNameValue, new ArrayList());
				mapAL_parent.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), newNode);
				mapAL_child.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), sectionNameValue);
				instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_child.get(parentValue).indexOf(sectionNameValue), mapAL_child.get(parentValue).indexOf(sectionNameValue), org.zkoss.zul.event.TreeDataEvent.CONTENTS_CHANGED);
				title.setValue(_edit + " Lecture Section Successful");
				instance_tree.clearSelection();
				try{
					contents.removeChild(contents.getFirstChild());
				} catch (Exception e) { }
			}
			else{
				alert("Check information supplied");
			}
			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	else if (type.equals("Laboratory")){
		try{
			String courseCodeValue;
			String sectionNameValue;
			String labNameValue;
			String dayValue; 
			String timeValue;
			String roomValue;
			String instructorValue;
			Integer studentCountValue;
			
			if (course_code.value.trim().length() != 0)
				courseCodeValue = course_code.value;
			else{
				alert("Invalid course");
				return;
			}
			
			if (section_name.value.trim().length() != 0)
				sectionNameValue = section_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			if (laboratory_name.value.trim().length() != 0)
				labNameValue = laboratory_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			String labName = sectionNameValue +"-"+labNameValue;
			if (day.value.trim().length() != 0)
				dayValue = day.value;
			else{
				alert("Invalid day");
				return;
			}
			
			
			if(!start_hrs.getValue().equals("") && !start_mins.getValue().equals("") && !end_hrs.getValue().equals("") && !end_mins.getValue().equals("")){
				if(start_mins.getValue().equals("00"))
					timeValue = start_hrs.getValue();
				else
					timeValue = start_hrs.getValue() + ":" + start_mins.getValue();
					
				if(end_mins.getValue().equals("00"))
					timeValue = timeValue + "-" + end_hrs.getValue();
				else
					timeValue = timeValue + "-" + end_hrs.getValue() + ":" + end_mins.getValue();
			}
			else{
				alert("Invalid time");
				return;
			}
			
			if (room.value.trim().length() != 0)
				roomValue = room.value;
			else{
				alert("Invalid room");
				return;
			}
			
			
			if (instructor.value.trim().length() != 0)
				instructorValue = instructor.value;
			else{
				alert("Invalid instructor");
				return;
			}
			
			
			if (student_count.value != null && student_count.value > 0)
				studentCountValue = student_count.value;
			else{
				alert("Invalid number");
				return;
			}
			
			if (new SectionPeer().updateLaboratorySection(section.getSectionId(), labName, dayValue, roomValue, timeValue, instructorValue, studentCountValue, team_teach.getSelectedIndex())){
				String parentValue;
				parentValue = course_code.value;
			
				SimpleTreeNode newNode = new SimpleTreeNode(labName, new ArrayList());
				mapAL_parent.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), newNode);
				mapAL_child.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), labName);
				instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_child.get(parentValue).indexOf(labName), mapAL_child.get(parentValue).indexOf(labName), org.zkoss.zul.event.TreeDataEvent.CONTENTS_CHANGED);
				title.setValue(_edit + " Laboratory Section Successful");
				instance_tree.clearSelection();
				try{
					contents.removeChild(contents.getFirstChild());
				} catch (Exception e) { }
			}
			else{
				alert("Check information supplied");
			}
		}catch(Exception e){
			e.printStackTrace();
			alert("Check information supplied");
		}
	}
	
	else if (type.equals("Recitation")){
		try{
			String courseCodeValue;
			String sectionNameValue;
			String labNameValue;
			String dayValue; 
			String timeValue;
			String roomValue;
			String instructorValue;
			Integer studentCountValue;
			
			if (course_code.value.trim().length() != 0)
				courseCodeValue = course_code.value;
			else{
				alert("Invalid course");
				return;
			}
			
			if (section_name.value.trim().length() != 0)
				sectionNameValue = section_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			if (laboratory_name.value.trim().length() != 0)
				labNameValue = laboratory_name.value;
			else{
				alert("Invalid section name");
				return;
			}
			
			String labName = sectionNameValue +"-"+labNameValue;
			if (day.value.trim().length() != 0)
				dayValue = day.value;
			else{
				alert("Invalid day");
				return;
			}
			
			
			if(!start_hrs.getValue().equals("") && !start_mins.getValue().equals("") && !end_hrs.getValue().equals("") && !end_mins.getValue().equals("")){
				if(start_mins.getValue().equals("00"))
					timeValue = start_hrs.getValue();
				else
					timeValue = start_hrs.getValue() + ":" + start_mins.getValue();
					
				if(end_mins.getValue().equals("00"))
					timeValue = timeValue + "-" + end_hrs.getValue();
				else
					timeValue = timeValue + "-" + end_hrs.getValue() + ":" + end_mins.getValue();
			}
			else{
				alert("Invalid time");
				return;
			}
			
			if (room.value.trim().length() != 0)
				roomValue = room.value;
			else{
				alert("Invalid room");
				return;
			}
			
			
			if (instructor.value.trim().length() != 0)
				instructorValue = instructor.value;
			else{
				alert("Invalid instructor");
				return;
			}
			
			
			if (student_count.value != null && student_count.value > 0)
				studentCountValue = student_count.value;
			else{
				alert("Invalid number");
				return;
			}
			
			if (new SectionPeer().updateRecitationSection(section.getSectionId(), labName, dayValue, roomValue, timeValue, instructorValue, studentCountValue, team_teach.getSelectedIndex())){
				String parentValue;
				parentValue = course_code.value;
			
				SimpleTreeNode newNode = new SimpleTreeNode(labName, new ArrayList());
				mapAL_parent.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), newNode);
				mapAL_child.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), labName);
				instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_child.get(parentValue).indexOf(labName), mapAL_child.get(parentValue).indexOf(labName), org.zkoss.zul.event.TreeDataEvent.CONTENTS_CHANGED);
				title.setValue(_edit + " Recitation Section Successful");
				instance_tree.clearSelection();
				try{
					contents.removeChild(contents.getFirstChild());
				} catch (Exception e) { }
			}
			else{
				alert("Check information supplied");
			}
		}catch(Exception e){
			e.printStackTrace();
			alert("Check information supplied");
		}
	}
}

/* [start]	deletes an entry */
void delete(){
	if(instance_tree.getSelectedCount() != 0){
		int isConfirmed = Messagebox.show(_proceed, _delete + " Secion", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			boolean isError = false;
			int errorCount = 0;
			Iterator i = instance_tree.getSelectedItems().iterator();
			List courseCodes = new Vector();
			List sectionNames = new Vector();
			while(i.hasNext()){
				Treeitem treeitem = i.next(); 
				Section temp = new SectionPeer().getSection(config_id.value,new CoursePeer().getCourseId(treeitem.getParent().getParent().getLabel()), treeitem.getLabel());
				if (temp != null){
					if(temp.getLaboratoryDay() == null  && temp.getRecitationDay() == null ){
						List l = new SectionPeer().getLaboratory(config_id.value, temp.getCourseId(), temp.getSectionName());
						if(l.size()>0){
							isError = true;
							errorCount++;
							continue;
						}
					}
					if (new SectionPeer().deleteSection(temp.getSectionId())){
						courseCodes.add(new CoursePeer().getCourse(temp.getCourseId()).getCourseCode());
						sectionNames.add(temp.getSectionName());
					}
					else{
						isError = true;
						errorCount++;
					}
				}
			}
			for(int i=0; i<courseCodes.size(); i++){
				String parentValue;
				parentValue = courseCodes.get(i);
				int index = mapAL_child.get(parentValue).indexOf(sectionNames.get(i));
				mapAL_parent.get(parentValue).remove(index);
				mapAL_child.get(parentValue).remove(index);
				instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), index, index, org.zkoss.zul.event.TreeDataEvent.INTERVAL_REMOVED);
				if (mapSTN_parent.get(parentValue).isLeaf())
					mapAL_parent.get(parentValue).add(new SimpleTreeNode("", new ArrayList()));
			}
			if (isError){ 
				alert(_delete + " " + _failed + ": " + errorCount + " " + "Section");
				title.setValue(_delete + " " + "Section" + " " + _failed);
			}
			else{
				title.setValue(_delete + " " + "Section" + " " + _successful);
				if (contents.getFirstChild() != null){
					contents.removeChild(contents.getFirstChild());
				}
			}
		}			
	}
	else{
		alert(_select + " " + _instance);
	}
}

void filterCourse(String string){
	ListIterator i = courses.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next().getCourseCode(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	course_code_lb.getModel().clear();
	course_code_lb.getModel().addAll(list);
}

void importSections(){
	org.zkoss.util.media.Media media = Fileupload.get();
	if(media != null){
		java.io.Reader r = new java.io.BufferedReader(media.getReaderData());
		String line = null;
		StringBuffer buf = new StringBuffer(9999999);

		while ((line = r.readLine()) != null) {
			buf.append(line+"\r\n"); 
		}
		new registnet.dao.SectionPeer().importSection(buf.toString(), config_id.value);
	}
}
/* [end] */


